using System.CodeDom;
using CodeDomHelper.Types;
using System;
using System.Diagnostics;

namespace CodeDomHelper.CodeDom
{

    [DebuggerDisplay("{TypeExpression}", Name = "CdeType")]
    [Serializable()]    
    public partial class TypeExp : CodeTypeReference
    {


        #region Ctor
        
        public TypeExp(TypeExpression type)    
            : base(type.FullName)
        {
            TypeExpression = type.Clone();
            Clean();
        }

        private void Clean(CodeTypeReference type = null)
        {

            if (type == null)
                type = this;

            if (type.TypeArguments != null)
                foreach (CodeTypeReference item in type.TypeArguments)
                {
                    Clean(item);
                    item.BaseType = item.BaseType.Replace("[", "").Replace("]", "");
                }

        }


        public TypeExp(TypeExpression type, CodeTypeReferenceOptions option)
            : this(type)
        {


        }


        public TypeExp(TypeExp type)
            : this(type.TypeExpression)
        {


        }


        public TypeExp(TypeExp type, CodeTypeReferenceOptions option)
            : this(type.TypeExpression, option)
        {
             
        }

        public TypeExp(Type type)
            : base(type)
        {
            string txt = type.AssemblyQualifiedName;
            TypeExpression = BuildTypeExpression.Analyse(txt);
        }

        public TypeExp(Type type, CodeTypeReferenceOptions option)
            : base(type, option)
        {
            TypeExpression = BuildTypeExpression.Analyse(type.AssemblyQualifiedName);            
        }

        public TypeExp(string type)
            : this(BuildTypeExpression.Analyse(type))
        {
           
        }

        public TypeExp(string type, CodeTypeReferenceOptions option)
            : this(BuildTypeExpression.Analyse(type), option)
        {

        }

        #endregion


        public TypeExpression TypeExpression { get; private set; }

        public CodeTypeReferenceExpression ReferenceExpression { get { return new CodeTypeReferenceExpression(this); } }

        public CodeTypeOfExpression TypeOf { get { return new CodeTypeOfExpression(this); } }

        public static Type Object { get { return typeof(Object); } }

        public static TypeExp Void { get { return null; } }

        public CodeObjectCreateExpression New(params CodeExpression[] parameters) { return new CodeObjectCreateExpression(this, parameters); }

        public static CodeExpression Null { get { return new CodePrimitiveExpression(null); } }

        public CodeDefaultValueExpression DefaultValue { get { return new CodeDefaultValueExpression(this); } }
      
    }

}
